package com.web.dao;

import com.web.entity.Goods;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author FDjavaone
 */
public interface GoodsMapper {


    @Delete({
            "delete from t_goods",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into t_goods (menu_id, title,",
            " image, price, num, ",
            " create_time, modify_time) ",
            " values (#{menuId,jdbcType=BIGINT}, #{title,jdbcType=VARCHAR}, ",
            " #{image,jdbcType=VARCHAR}, #{price,jdbcType=DECIMAL}, #{num,jdbcType=INTEGER}, ",
            " #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insert(Goods record);

    @InsertProvider(type = GoodsSqlProvider.class, method = "insertSelective")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = Long.class)
    int insertSelective(Goods record);


    @Select({
            "select",
            " id, menu_id, title, image, price, num, create_time, modify_time ",
            " from t_goods ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "menu_id", property = "menuId", jdbcType = JdbcType.BIGINT),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "image", property = "image", jdbcType = JdbcType.VARCHAR),
            @Result(column = "price", property = "price", jdbcType = JdbcType.DECIMAL),
            @Result(column = "num", property = "num", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Goods selectByPrimaryKey(Long id);


    @UpdateProvider(type = GoodsSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Goods record);

    @Update({
            "update t_goods",
            " set menu_id = #{menuId,jdbcType=BIGINT},",
            " title = #{title,jdbcType=VARCHAR},",
            " image = #{image,jdbcType=VARCHAR},",
            " price = #{price,jdbcType=DECIMAL},",
            " num = #{num,jdbcType=INTEGER},",
            " create_time = #{createTime,jdbcType=TIMESTAMP},",
            " modify_time = #{modifyTime,jdbcType=TIMESTAMP} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Goods record);

    @Select({
            "select",
            " id, menu_id, title, image, price, num, create_time, modify_time ",
            " from t_goods ",
            " where menu_id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "menu_id", property = "menuId", jdbcType = JdbcType.BIGINT),
            @Result(column = "title", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "image", property = "image", jdbcType = JdbcType.VARCHAR),
            @Result(column = "price", property = "price", jdbcType = JdbcType.DECIMAL),
            @Result(column = "num", property = "num", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "modify_time", property = "modifyTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Goods> selectByMenuId(Long id);

    @Delete({
            "delete from t_goods ",
            " where menu_id = #{id,jdbcType=BIGINT}"
    })
    void deleteByMenuId(Long id);


    @Update({
            "update t_goods",
            " set",
            " num =num+#{num,jdbcType=DECIMAL} ",
            " where id = #{id,jdbcType=BIGINT}"
    })
    void numAdd(@Param("num") Integer num,@Param("id") Long id);

    @Select({
            "select",
            " menu_id ",
            " from t_goods",
            " where title like #{title,jdbcType=VARCHAR}"
    })
    List<Long> selectByTitle(String s);
}